﻿
namespace VMWareLabWorkflowsDataContracts
{
    using System;
    using System.ComponentModel;
    using System.Drawing.Design;
    using System.Windows.Forms;
    using System.Windows.Forms.Design;
    using Microsoft.TeamFoundation.Build.Client;
    using VMWareLabWorkflowsDataContracts.VMWareLabDetailsDialogControls.Commands;

    public class VMWareLabDetailEditor : UITypeEditor
    {
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            object oldValue = ObjectCloner.Clone((VMWareLabWorkflowDetails)value);
            if (provider != null)
            {
                IWindowsFormsEditorService editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

                if (editorService != null)
                {
                    VMWareLabWorkflowDetails vmWareworkflowDetails = value as VMWareLabWorkflowDetails;
                    IBuildDefinition bd = (IBuildDefinition)provider.GetService(typeof(IBuildDefinition));
                    VMWareWizardData.VMWareWizzardDataInstance.VMWareLabWorkflowDetails = vmWareworkflowDetails;
                    VMWareWizardData.VMWareWizzardDataInstance.BuildDefinition = bd;
                    VMWareWizardData.VMWareWizzardDataInstance.ServiceProvider = provider;

                    using (VMWareLabDetailsDialog dialog = new VMWareLabDetailsDialog())
                    {
                        if (editorService.ShowDialog(dialog) == DialogResult.OK)
                        {
                            // Workaround to mark the build definition as dirty, to promt for save. Clone the value to get a new object.
                            object returnValue = ObjectCloner.Clone((VMWareLabWorkflowDetails)value);
                            return returnValue;
                        }
                    }
                }
            }

            return oldValue;
        }

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }
    }
}
